<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script>
      run(async function () {
        const ponyfill = lolex.createClock();
        const { directLine, store } = testHelpers.createDirectLineEmulator({ ponyfill });

        WebChat.renderWebChat(
          {
            directLine,
            ponyfill,
            store,
            styleOptions: {
              typingAnimationBackgroundImage: `url('')`
            }
          },
          document.querySelector('main')
        );

        await pageConditions.webChatRendered();

        // Wait for "Connecting..." message to dismiss
        ponyfill.tick(600);

        await pageConditions.uiConnected();

        await directLine.emulateIncomingActivity({
          channelData: { webChat: { styleOptions: { typingAnimationDuration: 10_000 } } },
          type: 'typing'
        });

        // THEN: Should show typing indicator initially.
        await pageConditions.typingIndicatorShown();

        // WHEN: At t=5s.
        ponyfill.tick(5_000);
        // THEN: Should show typing indicator.
        await pageConditions.typingIndicatorShown();

        // WHEN: At t=10s.
        ponyfill.tick(5_000);
        // THEN: Should hide typing indicator.
        await pageConditions.typingIndicatorHidden();

        // WHEN: Another "typing" activity is received with typing animation duration of 2s.
        await directLine.emulateIncomingActivity({
          channelData: { webChat: { styleOptions: { typingAnimationDuration: 2_000 } } },
          type: 'typing'
        });
        // THEN: Should show typing indicator initially.
        await pageConditions.typingIndicatorShown();

        // WHEN: At t=2s.
        ponyfill.tick(2_000);
        // THEN: Should hide typing indicator.
        await pageConditions.typingIndicatorHidden();

        // WHEN: Another "typing" activity is received with typing animation duration of "Infinity".
        await directLine.emulateIncomingActivity({
          channelData: { webChat: { styleOptions: { typingAnimationDuration: 'Infinity' } } },
          type: 'typing'
        });
        // THEN: Should show typing indicator initially.
        await pageConditions.typingIndicatorShown();

        // WHEN: At t=60s.
        ponyfill.tick(60_000);
        // THEN: Should show typing indicator.
        await pageConditions.typingIndicatorShown();

        // WHEN: Another "typing" activity is received with typing animation duration of 1s.
        await directLine.emulateIncomingActivity({
          channelData: { webChat: { styleOptions: { typingAnimationDuration: 1_000 } } },
          type: 'typing'
        });
        // THEN: Should show typing indicator initially.
        await pageConditions.typingIndicatorShown();

        // WHEN: At t=1s.
        ponyfill.tick(1_000);
        // THEN: Should show typing indicator.
        await pageConditions.typingIndicatorHidden();

        // WHEN: Another "typing" activity is received with typing animation duration of 0s.
        await directLine.emulateIncomingActivity({
          channelData: { webChat: { styleOptions: { typingAnimationDuration: 0 } } },
          type: 'typing'
        });
        // THEN: Should show typing indicator initially.
        await pageConditions.typingIndicatorHidden();

        // WHEN: Another "typing" activity is received with typing animation duration of "Infinity".
        await directLine.emulateIncomingActivity({
          channelData: { webChat: { styleOptions: { typingAnimationDuration: 'Infinity' } } },
          type: 'typing'
        });
        // THEN: Should show typing indicator initially.
        await pageConditions.typingIndicatorShown();

        // WHEN: A "message" activity is received while an infinite typing indicator is shown.
        await directLine.emulateIncomingActivity({
          text: 'Ex est exercitation reprehenderit pariatur veniam aute id culpa ad esse aute.',
          type: 'message'
        });
        // THEN: Should hide the typing indicator.
        await pageConditions.typingIndicatorHidden();
      });
    </script>
  </body>
</html>
